package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.Jbqk;
import com.corpcredit.model.Zmsb;
import com.corpcredit.service.GenericManager;

public class ZmsbForm extends BasePage implements Serializable {
    private GenericManager<Zmsb, Long> zmsbManager;
    private Zmsb zmsb = new Zmsb();
    private Long id;
    private Jbqk jbqk;

    public void setJbqk( Jbqk jbqk )
    {
        this.jbqk = jbqk;
    }

    public void setZmsbManager(GenericManager<Zmsb, Long> zmsbManager) {
        this.zmsbManager = zmsbManager;
    }

    public Zmsb getZmsb() {
        return zmsb;
    }

    public void setZmsb(Zmsb zmsb) {
        this.zmsb = zmsb;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        zmsbManager.remove(zmsb.getId());
        addMessage("zmsb.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            zmsb = zmsbManager.get(id);
        } else {
            zmsb = new Zmsb();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (zmsb.getId() == null || zmsb.getId() == 0);
        zmsb.setJbqk( jbqk );
        zmsb.setUsername(getRequest().getRemoteUser());
        zmsb.setOptdate(new Date());
        zmsb.setOperation( (isNew) ? Constants.OPERATION_ADD : Constants.OPERATION_UPDATE );
        zmsbManager.save(zmsb);

        String key = (isNew) ? "zmsb.added" : "zmsb.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
        	this.id = zmsb.getId();
            return "edit";
        }
    }
} 